package ogmios.components.munches;

import ogmios.core.*;
import ogmios.components.*;
import ogmios.components.templatetags.*;
import ogmios.dialogs.*;
import java.util.*;
import java.io.*;


/**
 * The munch containing information on how to construct a Command
 */
public class CommandMunch implements Munch
{
 public String getTemplate()
 {
  return  "package commands;\n\n" + 
              "import ogmios.core.*;\n" +
              "import ogmios.components.munches.*;\n" + 
              "import java.util.List;\n" + 
              "import java.awt.*;\nimport java.awt.event.*;\n" +
              "import javax.swing.*;\n\n" +
              "public class {name} implements Command\n" +
              "{\n\n" +
              " public String getManPage()\n" +
              " {\n\n" +
              " }\n\n" +
              " public void execute(Context shell, List<String> args) throws Throwable\n" +
              " {\n\n" +
              " }\n\n" +
              "}";
 }

 public Set<TemplateTag> getTemplateTags()
 {
  return TemplateManager.DEFAULT_TEMPLATE_TAGS;
 }

 public String getFolder()
 {
  return "commands";
 }

 public String getName()
 {
  return "Command";
 }

 public Map<String, MunchSetting> getSettings()
 {
  Map<String, MunchSetting> settings = new LinkedHashMap<String, MunchSetting>();
  settings.put("Classpath", MunchSetting.STRING);
  settings.put("Sourcepath", MunchSetting.STRING);
  settings.put("Compiler Arguments", MunchSetting.ARRAY);
  return settings;
 }

 public Class getMunchType()
 {
  return Command.class;
 }

 public Process compile(String fileName, Map<String, Object> settings) throws IOException
 {
  String classpath = (String)settings.get("Classpath");
  String sourcepath = (String)settings.get("Sourcepath");
  String userDir = System.getProperty("user.dir") + File.separator;
  String[] compilerArgs = (String[])settings.get("Compiler Arguments");
  String path = userDir + getFolder(); 
  String[] args = new String[8 + compilerArgs.length];
  args[0] = Ogmios.jAddress + File.separator + "javac";
  args[1] = "-classpath";
  args[2] = path + File.pathSeparator + userDir + "ogmios.jar" + File.pathSeparator + classpath;
  args[3] = "-sourcepath";
  args[4] = sourcepath + File.pathSeparator;
  args[5] = "-d";
  args[6] = userDir;
  System.arraycopy(compilerArgs, 0, args, 7, compilerArgs.length);
  args[args.length - 1] =path + File.separator + "code" + File.separator + fileName + ".java";
  return Runtime.getRuntime().exec(args); 
 }
}
